﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System.Collections.Generic;
using System.Linq;
using pesta.Data.Model.Helpers;
using Pesta.DataAccess.Azure;
using raya.Library;

namespace raya.Models.notifications
{
    public class ProfileNotification
    {
#if AZURE
        public List<NotificationRow> notifyList { get; set; }
#else
        public List<Notification> notifyList { get; set; }
#endif
        public Dictionary<notifications.notifyType,int> count { get; set; }
    }
    public class Notification
    {
        public int creator { get; set; }
        public string creatorName { get; set; }
        public int recipient { get; set; }
        public int type { get; set; }
    }

    public class notifications : Model
    {
        public enum notifyType
        {
            friendRequest = 1,
            friendRecommend,
            information
        }

#if AZURE
        public List<NotificationRow> GetNotifications(string pid)
        {
            List<NotificationRow> result = new List<NotificationRow>();
            using (var db = RayaDataContext.DB())
            {
                var req = from f in db.notifications
                          where f.recipient == pid
                          select f;
                foreach (var r in req)
                {
                    result.Add(r);
                }
            }
            return result;
        }
#else
        public List<Notification> GetNotifications(int pid)
        {
            List<Notification> result = new List<Notification>();
            using (var db = RayaDataContext.DB())
            {
                var req = from f in db.notifications
                          where f.recipient == pid
                          select f;
                foreach (var r in req)
                {
                    result.Add(new Notification
                    {
                        creatorName = r.person.first_name + " " + r.person.last_name,
                        creator = r.creator,
                        recipient = pid,
                        type = r.type
                    });
                }
            }
            return result;
        }

        public List<Notification> GetFriendRequests(int pid)
        {
            List<Notification> result = new List<Notification>();
            using (var db = RayaDataContext.DB())
            {
                var req = from f in db.notifications
                          where f.recipient == pid && f.type == notifyType.friendRequest.ToInt()
                          select new
                          {
                              f.creator,
                              f.person.first_name,
                              f.person.last_name
                          };
                foreach (var r in req)
                {
                    result.Add(new Notification
                                   {
                                        creatorName = r.first_name + " " + r.last_name,
                                        creator = r.creator,
                                        recipient = pid,
                                        type = (int)notifyType.friendRequest
                                    });
                }
            }
            return result;
        }
#endif

        
    }
}